package com.abvnet.project.water.project.controller;

import com.abvnet.common.utils.poi.ExcelUtil;
import com.abvnet.common.utils.security.ShiroUtils;
import com.abvnet.framework.aspectj.lang.annotation.Log;
import com.abvnet.framework.aspectj.lang.enums.BusinessType;
import com.abvnet.framework.web.controller.BaseController;
import com.abvnet.framework.web.domain.AjaxResult;
import com.abvnet.framework.web.page.TableDataInfo;
import com.abvnet.project.system.config.service.IConfigService;
import com.abvnet.project.water.project.domain.ProjectInfo;
import com.abvnet.project.water.project.domain.ProjectLog;
import com.abvnet.project.water.project.domain.ProjectLogMicro;
import com.abvnet.project.water.project.service.IProjectInfoService;
import com.abvnet.project.water.project.service.IProjectLogService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 项目日志信息操作处理
 * 
 * @author duj
 * @date 2019-07-19
 */
@Controller
@RequestMapping("/project/projectLog")
public class ProjectLogController extends BaseController {
    private String prefix = "project/projectLog";

	@Autowired
	private IProjectInfoService projectInfoService;
	@Autowired
	private IProjectLogService projectLogService;
	@Autowired
	private IConfigService configService;

	/**
	 * 查询项目列表
	 */
	@RequiresPermissions("project:projectLog:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(ProjectInfo projectInfo) {
		if (!ShiroUtils.getSysUser().isAdmin()) { // 非管理员，只有数据可见中的用户能看到
			projectInfo.setCreateBy(ShiroUtils.getLoginName());
			projectInfo.setVisibleUserId(String.valueOf(ShiroUtils.getUserId()));
		}
		startPage();
		List<ProjectInfo> list = projectInfoService.selectProjectInfoList(projectInfo);
		return getDataTable(list);
	}
	
	@RequiresPermissions("project:projectLog:list")
	@GetMapping()
	public String projectLog()
	{
	    return prefix + "/list";
	}
	
	/**
	 * 查询项目日志列表
	 */
	@RequiresPermissions("project:projectLog:list")
	@PostMapping("/logList")
	@ResponseBody
	public TableDataInfo logList(ProjectLog projectLog) {
		startPage();
        List<ProjectLog> list = projectLogService.selectProjectLogList(projectLog);
		return getDataTable(list);
	}

	@RequiresPermissions("project:projectLog:list")
	@GetMapping("/logList/{projectId}")
	public String logList(@PathVariable("projectId") Long projectId, ModelMap mmap) {
		ProjectInfo projectInfo = new ProjectInfo();
		projectInfo.setProjectId(projectId);
		mmap.put("projectInfo", projectInfo);
		return prefix + "/logList";
	}

    @RequiresPermissions("project:projectLog:list")
    @GetMapping("/detail/{logId}")
    public String detail(@PathVariable("logId") Long logId, ModelMap mmap) {
        mmap.put("projectLog", projectLogService.selectProjectLogById(logId));
        return prefix + "/detail";
    }

    @RequiresPermissions("project:projectLog:list")
    @PostMapping("/microList")
    @ResponseBody
    public TableDataInfo microList(Long logId) {
        List<ProjectLogMicro> projectLogMicros = projectLogService.selectProjectLogMicroByLogId(logId);
        return getDataTable(projectLogMicros);
    }

	/**
	 * 新增项目日志
	 */
	@GetMapping("/add")
	public String add() {
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存项目日志
	 */
	@RequiresPermissions("project:projectLog:add")
	@Log(title = "项目日志", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(ProjectLog projectLog) {
		return toAjax(projectLogService.insertProjectLog(projectLog));
	}

	/**
	 * 修改项目日志
	 */
	@GetMapping("/edit/{logId}")
	public String edit(@PathVariable("logId") Long logId, ModelMap mmap) {
		ProjectLog projectLog = projectLogService.selectProjectLogById(logId);
		mmap.put("projectLog", projectLog);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存项目日志
	 */
	@RequiresPermissions("project:projectLog:edit")
	@Log(title = "项目日志", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(ProjectLog projectLog) {
		return toAjax(projectLogService.updateProjectLog(projectLog));
	}
	
	/**
	 * 删除项目日志
	 */
	@RequiresPermissions("project:projectLog:remove")
	@Log(title = "项目日志", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids) {
		return toAjax(projectLogService.deleteProjectLogByIds(ids));
	}

	/**
	 * 导出日志
	 * @param projectLog
	 * @return
	 */
	@RequiresPermissions("project:projectLog:edit")
	@PostMapping("/export")
	@ResponseBody
	public AjaxResult export(ProjectLog projectLog) {
		List<ProjectLog> list = projectLogService.selectProjectLogList(projectLog);
		for (ProjectLog log : list)
			log.setLogDateStr(new SimpleDateFormat("yyyy-MM-dd").format(log.getLogDate()));
		ExcelUtil<ProjectLog> util = new ExcelUtil<ProjectLog>(ProjectLog.class);
		return util.exportExcel(list, "日志数据");
	}

	/**
	 * 下载项目日志模板
	 * @return
	 */
	@RequiresPermissions("project:projectLog:edit")
	@GetMapping("/importTemplate")
	@ResponseBody
	public AjaxResult importTemplate() {
		ExcelUtil<ProjectLog> util = new ExcelUtil<>(ProjectLog.class);
		return util.importTemplateExcel("日志模板");

//		List<ProjectLog> list = new ArrayList<>();
//		ProjectLog log = new ProjectLog();
//		log.setLogDateStr(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
//		log.setLogWeather("晴");
//		log.setInIntake(20000);
//		log.setOutIntake(25000);
//		log.setInCod(BigDecimal.valueOf(2000));
//		log.setInNh3N(BigDecimal.valueOf(2000));
//		log.setInTotalN(BigDecimal.valueOf(2000));
//		log.setInTotalP(BigDecimal.valueOf(2000));
//		log.setInPh(BigDecimal.valueOf(2000));
//		log.setInSv30(BigDecimal.valueOf(2000));
//		log.setInDo(BigDecimal.valueOf(2000));
//		log.setInTp(20);
//		log.setOutCod(BigDecimal.valueOf(2500));
//		log.setOutNh3N(BigDecimal.valueOf(2500));
//		log.setOutTotalN(BigDecimal.valueOf(2500));
//		log.setOutTotalP(BigDecimal.valueOf(2500));
//		log.setOutPh(BigDecimal.valueOf(2500));
//		log.setOutSv30(BigDecimal.valueOf(2500));
//		log.setOutDo(BigDecimal.valueOf(2500));
//		log.setOutTp(25);
//		log.setLogProblem("存在的问题");
//		log.setLogAdjust("突发情况及应对说明");
//		log.setLogSudden("突发情况及应对说明");
//		log.setLogWorkPlan("明天的工作计划");
//		list.add(log);
//		ExcelUtil<ProjectLog> util = new ExcelUtil<ProjectLog>(ProjectLog.class);
//		return util.exportExcel(list, "日志模板");
	}

	/**
	 * 导入数据
	 * @param projectLogFile
	 * @param updateSupport
	 * @return
	 * @throws Exception
	 */
	@RequiresPermissions("project:projectLog:edit")
	@PostMapping("/uploadData")
	@ResponseBody
	public AjaxResult uploadData(@RequestParam("projectId") Long projectId,
								 @RequestParam("projectLogFile") MultipartFile projectLogFile, boolean updateSupport) throws Exception {
		ExcelUtil<ProjectLog> util = new ExcelUtil<ProjectLog>(ProjectLog.class);
		List<ProjectLog> projectLogList = util.importExcel(projectLogFile.getInputStream());
		String message = projectLogService.importProjectLog(projectId, projectLogList);
		return AjaxResult.success(message);
	}
}
